<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="密保问题" @ok="handleSubmit" width="1000px">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup name="user-secure-question-modal">
  import { ref, unref, defineExpose } from 'vue';
  import { rules } from '/@/utils/helper/validator';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { isNullOrUnDef, isEmpty } from '/@/utils/is';
  import { updateUserQuestion } from '../UserSetting.api';
  // 声明Emits
  const emit = defineEmits(['register', 'success']);
  const $message = useMessage();
  const username = ref('');
  const hasPwdQuestion = (value) => !isNullOrUnDef(value) && !isEmpty(value);
  //表单配置
  const [registerForm, { setFieldsValue, resetFields, validate, clearValidate }] = useForm({
    schemas: [
      {
        field: 'divider-basic',
        component: 'Divider',
        label: '您还未设置过密保问题，您可以根据登录密码设置新的密保问题及答案。',
        ifShow: ({ values }) => !hasPwdQuestion(values.oldPwdQuestion),
      },
      {
        label: '登录密码',
        field: 'loginPassword',
        component: 'InputPassword',
        required: true,
        ifShow: ({ values }) => !hasPwdQuestion(values.oldPwdQuestion),
      },
      {
        field: 'divider-basic',
        component: 'Divider',
        label: '旧的密保问题及答案',
        ifShow: ({ values }) => hasPwdQuestion(values.oldPwdQuestion),
      },
      {
        label: '旧密保问题(1)',
        field: 'oldPwdQuestion',
        component: 'Input',
        required: true,
        ifShow: ({ values }) => hasPwdQuestion(values.oldPwdQuestion),
      },
      {
        label: '旧密保问题答案(1)',
        field: 'oldPwdQuestionAnswer',
        component: 'Input',
        required: true,
        ifShow: ({ values }) => hasPwdQuestion(values.oldPwdQuestion),
      },
      {
        label: '旧密保问题(2)',
        field: 'oldPwdQuestionTwo',
        component: 'Input',
        required: true,
        ifShow: ({ values }) => hasPwdQuestion(values.oldPwdQuestion),
      },
      {
        label: '旧密保问题答案(2)',
        field: 'oldPwdQuestionAnswerTwo',
        component: 'Input',
        required: true,
        ifShow: ({ values }) => hasPwdQuestion(values.oldPwdQuestion),
      },
      {
        label: '旧密保问题(3)',
        field: 'oldPwdQuestionThree',
        component: 'Input',
        required: true,
        ifShow: ({ values }) => hasPwdQuestion(values.oldPwdQuestion),
      },
      {
        label: '旧密保问题答案(3)',
        field: 'oldPwdQuestionAnswerThree',
        component: 'Input',
        required: true,
        ifShow: ({ values }) => hasPwdQuestion(values.oldPwdQuestion),
      },
      {
        field: 'divider-basic',
        component: 'Divider',
        label: '新的密保问题及答案',
      },
      {
        label: '新密保问题(1)',
        field: 'newPwdQuestion',
        component: 'Input',
        required: true,
      },
      {
        label: '新密保问题答案(1)',
        field: 'newPwdQuestionAnswer',
        component: 'Input',
        required: true,
      },
      {
        label: '新密保问题(2)',
        field: 'newPwdQuestionTwo',
        component: 'Input',
        required: true,
      },
      {
        label: '新密保问题答案(2)',
        field: 'newPwdQuestionAnswerTwo',
        component: 'Input',
        required: true,
      },
      {
        label: '新密保问题(3)',
        field: 'newPwdQuestionThree',
        component: 'Input',
        required: true,
      },
      {
        label: '新密保问题答案(3)',
        field: 'newPwdQuestionAnswerThree',
        component: 'Input',
        required: true,
      },
    ],
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner();

  //表单提交事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      let params = Object.assign(values);
      updateUserQuestion(params).then((res) => {
        if (res.success) {
          $message.createMessage.success(res.msg);
          //关闭弹窗
          closeModal();
          emit('success');
        } else {
          $message.createMessage.warning(res.msg);
        }
      });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }

  async function show(name, questions) {
    if (!name) {
      $message.createMessage.warning('当前系统无登录用户!');
      return;
    } else {
      username.value = name;
      await setModalProps({ visible: true });
      await resetFields();
      await clearValidate();
      if (questions && questions.question1) {
        await setFieldsValue({
          oldPwdQuestion: questions.question1,
          oldPwdQuestionTwo: questions.question2,
          oldPwdQuestionThree: questions.question3,
        });
      } else {
      }
    }
  }

  defineExpose({
    show,
  });
</script>
